#include "gpiodata.h"

#include "../logs/printlog.h"

#if GPIO_TEST
#include<wiringPi.h>

#endif

GpioData::GpioData(QObject *parent) : QObject(parent)
{

    initGpio();
}

GpioData::~GpioData()
{
    //digitalWrite(DIO1,HIGH);
}

void GpioData::initGpio()
{
#if GPIO_TEST
    wiringPiSetup();
    pinMode(GPIO_HEAT,OUTPUT);
    pinMode(GPIO_REFRI,OUTPUT);

    //    pinMode(GPIO_ELECTRIC_VALVE_ONE,OUTPUT);
    //    pinMode(GPIO_ELECTRIC_VALVE_TWO,OUTPUT);
    //    pinMode(GPIO_ELECTRIC_VALVE_THREE,OUTPUT);
    //    pinMode(GPIO_ELECTRIC_VALVE_FOUR,OUTPUT);
    //pinMode(GPIO_ELECTRIC_VALVE_FIVE,OUTPUT);
    pinMode(CO,OUTPUT);

    pinMode(D1,OUTPUT);
    pinMode(D2,OUTPUT);
    pinMode(D3,OUTPUT);
    pinMode(D4,OUTPUT);
    pinMode(VACCCM_PUMP,OUTPUT);
    pinMode(DEVICES,OUTPUT);

    digitalWrite(GPIO_HEAT,LOW);
    digitalWrite(GPIO_REFRI,LOW);

    //    digitalWrite(GPIO_ELECTRIC_VALVE_ONE,LOW);
    //    digitalWrite(GPIO_ELECTRIC_VALVE_TWO,LOW);
    //    digitalWrite(GPIO_ELECTRIC_VALVE_THREE,LOW);
    //    digitalWrite(GPIO_ELECTRIC_VALVE_FOUR,LOW);
    // digitalWrite(GPIO_ELECTRIC_VALVE_FIVE,LOW);


    digitalWrite(D1,LOW);
    digitalWrite(D2,LOW);
    digitalWrite(D3,LOW);
    digitalWrite(D4,LOW);
    digitalWrite(VACCCM_PUMP,LOW);
    digitalWrite(CO,LOW);
    digitalWrite(DEVICES,LOW);
#endif

    QString logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  +" contents ="+" pins";
    PrintLog::gestance()->printInfor(logStr);
}

void GpioData::openHeat()
{
#if GPIO_TEST
    digitalWrite(GPIO_HEAT,HIGH);
#endif
}

void GpioData::closeHeat()
{
#if GPIO_TEST
    digitalWrite(GPIO_HEAT,LOW);
#endif
}

void GpioData::openRefri()
{
#if GPIO_TEST
    digitalWrite(GPIO_REFRI,HIGH);
#endif
}

void GpioData::closeRefri()
{
#if GPIO_TEST
    digitalWrite(GPIO_REFRI,LOW);
#endif
}

int GpioData::getHeat()
{
    int status =0;
#if GPIO_TEST
    status = digitalRead(GPIO_HEAT);
#endif
    return status;
}

int GpioData::getRefri()
{
    int status =0;
#if GPIO_TEST
    status = digitalRead(GPIO_REFRI);
#endif
    return status;
}

void GpioData::handlDeviceStatus(int pins, int status)
{
#if GPIO_TEST

    switch (pins) {
    case 1:
        digitalWrite(GPIO_ELECTRIC_VALVE_ONE,status);
        break;
    case 2:
        digitalWrite(GPIO_ELECTRIC_VALVE_TWO,status);
        break;
    case 3:
        digitalWrite(GPIO_ELECTRIC_VALVE_THREE,status);
        break;
    case 4:
        digitalWrite(GPIO_ELECTRIC_VALVE_FOUR,status);
        break;
    case 5:
        digitalWrite(GPIO_ELECTRIC_VALVE_FIVE,status);
        break;


    }

#endif
}

GpioData *GpioData::gestance()
{
    static GpioData gpioData;

    return &gpioData;
}
void GpioData::openD1(){
    QString logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  ;
    PrintLog::gestance()->printInfor(logStr);
#if GPIO_TEST
    digitalWrite(D1,HIGH);
#endif
}
void GpioData::closeD1(){
    QString logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  ;
    PrintLog::gestance()->printInfor(logStr);
#if GPIO_TEST
    digitalWrite(D1,LOW);
#endif
}
int GpioData::getD1(){

    int status =0;
#if GPIO_TEST
    status = digitalRead(D1);
#endif
    return status;
}

void GpioData::openD2(){
    QString logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  ;
    PrintLog::gestance()->printInfor(logStr);
#if GPIO_TEST
    digitalWrite(D2,HIGH);
#endif
}
void GpioData::closeD2(){
    QString logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  ;
    PrintLog::gestance()->printInfor(logStr);
#if GPIO_TEST
    digitalWrite(D2,LOW);
#endif
}
int GpioData::getD2(){
    int status =0;
#if GPIO_TEST
    status = digitalRead(D2);
#endif
    return status;
}

void GpioData::openD3(){
    QString logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  ;
    PrintLog::gestance()->printInfor(logStr);
#if GPIO_TEST
    digitalWrite(D3,HIGH);
#endif
}
void GpioData::closeD3(){
    QString logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  ;
    PrintLog::gestance()->printInfor(logStr);
#if GPIO_TEST
    digitalWrite(D3,LOW);
#endif
}
int GpioData::getD3(){
    int status =0;
#if GPIO_TEST
    status = digitalRead(D3);
#endif
    return status;
}

void GpioData::openD4(){
    QString logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  ;
    PrintLog::gestance()->printInfor(logStr);
#if GPIO_TEST
    digitalWrite(D4,HIGH);
#endif
}
void GpioData::closeD4(){
    QString logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  ;
    PrintLog::gestance()->printInfor(logStr);
#if GPIO_TEST
    digitalWrite(D4,LOW);
#endif
}
int GpioData::getD4(){
    int status =0;
#if GPIO_TEST
    status = digitalRead(D4);
#endif
    return status;
}

void GpioData::openPump(){
#if GPIO_TEST
    digitalWrite(VACCCM_PUMP,HIGH);
#endif
}
void GpioData::closePump(){
#if GPIO_TEST
    digitalWrite(VACCCM_PUMP,LOW);
#endif
}
int GpioData::getPump(){
    int status =0;
#if GPIO_TEST
    status = digitalRead(VACCCM_PUMP);
#endif
    return status;
}

void GpioData::openCo()
{
#if GPIO_TEST
    digitalWrite(CO,HIGH);
#endif
}

void GpioData::closeCo()
{
#if GPIO_TEST
    digitalWrite(CO,LOW);
#endif
}

int GpioData::getCo()
{
    int status =0;
#if GPIO_TEST
    status = digitalRead(CO);
#endif
    return status;
}

void GpioData::openDevice()
{
#if GPIO_TEST
    digitalWrite(DEVICES,HIGH);
#endif
}

void GpioData::closeDevice()
{
#if GPIO_TEST
    digitalWrite(DEVICES,LOW);
#endif
}

int GpioData::getDevice()
{
    int status =0;
#if GPIO_TEST
    status = digitalRead(DEVICES);
#endif
    return status;
}
